package com.fsoft.finaltest.dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.fsoft.finaltest.dto.FoodOfAnimal;

public class FoodOfAnimalJDBCTemplate implements FoodOfAnimalDAO{
	private DataSource dataSource;
	private JdbcTemplate jdbcTemplateObject;
	
	@Override
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	    this.jdbcTemplateObject = new JdbcTemplate(this.dataSource);
	}

	@Override
	public void create(Integer animalID, String  foodType) {
		
		String SQL = "INSERT INTO Animal_Food(FoodID, FoodType) VALUES (?,?)";
	    jdbcTemplateObject.update( SQL, foodType, animalID);
	    return;
	}

	@Override
	public FoodOfAnimal getFoodOfAnimal(Integer id) {
		String SQL = "select * from Animal_Food where AnimalID = ?";
		FoodOfAnimal foodOfAnimal = jdbcTemplateObject.queryForObject(SQL, 
	                        new Object[]{id}, new FoodOfAnimalMapper());
	    return foodOfAnimal;
	}

	@Override
	public List<FoodOfAnimal> listFoodOfAnimal() {
		String SQL = "select * from Animal_Food";
	    List <FoodOfAnimal> foods = jdbcTemplateObject.query(SQL, 
	                                new FoodOfAnimalMapper());
	    return foods;	
	}

	@Override
	public void delete(Integer id) {
		String SQL = "delete from Animal_Food where AnimalID = ?";
	    jdbcTemplateObject.update(SQL, id);
	    return;
	}

	@Override
	public void update(Integer animalID, String  foodType) {
		String SQL = "update Food set FoodName = ? and FoodType = ? where FoodID = ?";
	    jdbcTemplateObject.update(SQL, foodType, animalID);
	    return;
	}
	
	
}
